<?php
/**
 * Class for hand authentication when 
 * login using OpenID
 * @author Tuan Ngo
 */

class AuthModel{
	function setIdentity($identity, $name, $user_id, $type){
		$session    = new Zend_Session_Namespace();
		$identity_arr = array(
			'identity' => $identity,
			'user_name' => $name,
            'user_id' => $user_id,
            'type_user' => $type
		);
		$session->identity = $identity_arr;
	}
	
	function hasIdentity(){
		$session = new Zend_Session_Namespace();
		return $session->identity ? true : false;
	}
	
	function getIdentity(){
		$session = new Zend_Session_Namespace();
		return $session->identity;
	}
	
	function clearIdentity(){
		$session = new Zend_Session_Namespace();
		unset($session->identity);
	}
	
	function getIdentityField($key){
		if($this->hasIdentity()){
			$identity = $this->getIdentity();
			return $identity[$key];
		}else 
			return NULL;
	}
	
	function validateAdmin($email){
		$result = preg_match("/(.)*@sunnet.vn/",trim($email));
		if($result){
			//check user is in user list or not
			$validator = new Zend_Validate_Db_RecordExists(array(
																	'table'=>'admin',
																	'field'=>'sunnet_email',
																	'exclude' => 'status=1'    
																	));
			if ($validator->isValid(trim($email))) {
				return true;
			}else 
				return false;
		}
		else 
			return false;
	}	
}